function [ecog bci]=bciRecordCalibData(lengthInSec)

% [ecog,bci]=bciRecordCalibData(lengthInSec)
% acquires Data of length lengthInSec (default: 10sec)
% and returns the data in ecog struct
% see also: bciPlotVariances, bciRawdata2Ecog, ecogTSGUI

if nargin<1,
    lengthInSec=10;
end

bci=bciGetParam();
fprintf('Start calibration recording.\n');
[hdr,bci]=bciReadRecorderHeader(bci);
bci=bciInit(bci);

startSample=hdr.nSamples;

startTime=GetSecs;
nextTrigSendTime=startTime;
trigCounter = 0;
while(GetSecs<startTime+lengthInSec),
    % send trigger each second
    if bci.param.doSendTrigger && GetSecs>nextTrigSendTime,
        bciSendTrigger(bci,mod(trigCounter,256));
        trigCounter = trigCounter+1;
        nextTrigSendTime=nextTrigSendTime+1;
    end
   pause(0.1); 
end

[hdr,bci]=bciReadRecorderHeader(bci);
ecog.data=double(bciReadRecorderData(bci, hdr, startSample, hdr.nSamples));
ecog.sampDur=1000/bci.srate;
ecog.selectedChannels=bci.preprocChan;
ecog.nSamp=size(ecog.data,2);
ecog.nBaselineSamp=0;
ecog.timebase=0:ecog.sampDur/1000:ecog.sampDur*(ecog.nSamp-1)/1000;

fprintf('Calibration recording finished.\n');
if bci.param.doReceiveTrigger,
    if length(bci.init.trigChan)>1,
        trigData = bciAnalog2Trig(ecog.data(bci.init.trigChan,:),bci.param.trigChanOrder);
    else
        trigData = ecog.data(bci.init.trigChan,:);
    end
    trigIdx = find(diff([0 trigData])>0);
    fprintf('Received trigger:\n');
    disp(trigData(trigIdx))
end